package com.hrms.controller.employee;

import com.hrms.annotation.EmployeeIgnore;
import com.hrms.entity.Employee;
import com.hrms.http.ResponseResult;
import com.hrms.service.EmployeeService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Transactional
@RestController
@RequestMapping("/employee/index")
public class EmployeeIndexController {
    // 员工首页

    @Autowired
    private EmployeeService employeeService;

    @EmployeeIgnore
    @RequestMapping("/login")
    public ResponseResult<Object> login(@RequestBody EntityUtil entity) {
        // 员工登录

        Employee employee = new Employee();
        employee.setEmployee_id(entity.getEmployee_id());
        employee.setEmployee_password(entity.getEmployee_password());

        // 工号是否存在
        if(!employeeService.idExist(employee)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_ACCOUNT", null);
        }

        // 工号密码是否匹配
        if(!employeeService.passwordisRight(employee)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_PASSWORD", null);
        }

        // 检查账号是否离线
        Employee employeeBefore = new Employee();
        employeeBefore.setEmployee_id(entity.getEmployee_id());
        employeeBefore.setEmployee_status("离线");

        if(!employeeService.checkStatue(employeeBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        Employee employeeAfter = new Employee();
        employeeAfter.setEmployee_id(entity.getEmployee_id());
        employeeAfter.setEmployee_status("在线");

        if(!employeeService.updateStatue(employeeAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        String employee_name = employeeService.selectEmployeeName(employee);

        return new ResponseResult<>("SUCCESS", employee_name);
    }

    @RequestMapping("/exit")
    public ResponseResult<Object> exit(@RequestBody EntityUtil entity) {
        // 员工退出
        Employee employee = new Employee();
        employee.setEmployee_id(entity.getEmployee_id());

        // 检查账号是否在线
        Employee employeeBefore = new Employee();
        employeeBefore.setEmployee_id(entity.getEmployee_id());
        employeeBefore.setEmployee_status("在线");

        if(!employeeService.checkStatue(employeeBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        // 修改账号状态
        Employee employeeAfter = new Employee();
        employeeAfter.setEmployee_id(entity.getEmployee_id());
        employeeAfter.setEmployee_status("离线");

        if(!employeeService.updateStatue(employeeAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }

}
